/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import GUI.Multilang.MyResources;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author ikmlop
 */
class DialogConnectToServer extends JDialog implements ActionListener
{

    private JTextField _txtUser;
    private JPasswordField _txtPass;
    private JTextField _txtServerAddress;
    private JTextField _txtPort;
    private Container contentPane;
    private SpringLayout springLayout = new SpringLayout();
    private frmClient _frmParent;
    private JTextField _txtChuoiKetNoi;///*************
    private JTextField _txtDatabase;///*************

    DialogConnectToServer(frmClient frmParent)
    {
        super(frmParent, "", true);
        _frmParent = frmParent;
        InitComponents();


        setVisible(false);
        setSize(350, 420);
        setResizable(false);
        setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private void InitComponents()
    {
        this.setTitle(MyResources.GetString("connecttoserver"));

        setLayout(springLayout);
        contentPane = this.getContentPane();
        contentPane.removeAll();
        int x = getWidth(MyResources.GetString("connecttoserver"));
        add(createLabel(MyResources.GetString("connecttoserver"), 105 - x / 2, 20));
        add(createSeparator(0, 35, 101 - x / 2, 5));
        add(createSeparator(109 + x / 2, 35, 241 - x / 2, 5));
        add(createSeparator(0, 360, 350, 5));///*************///*************

        add(createButton("OK", MyResources.GetString("connect"), 80, 310, 80, 26));
        add(createButton("Cancel", MyResources.GetString("cancel"), 200, 310, 80, 26));

        add(createLabel(MyResources.GetString("connectionString"), 35, 65));///*************
        add(createLabel(MyResources.GetString("serverip"), 35, 103));
        add(createLabel("Port", 35, 142));
        add(createLabel(MyResources.GetString("username"), 35, 180));
        add(createLabel(MyResources.GetString("password"), 35, 217));
        add(createLabel(MyResources.GetString("databasename"), 35, 255));///*************

        _txtChuoiKetNoi = createTextField(138, 62, 160, 24);///*************
        _txtChuoiKetNoi.setText("Tạm thời chưa dùng");
        _txtChuoiKetNoi.setEditable(false);
        _txtServerAddress = createTextField(138, 100, 160, 24);
        _txtPort = createTextField(138, 138, 160, 24);
        _txtUser = createTextField(138, 176, 160, 24);
        _txtPass = createPassWordField(138, 214, 160, 24);
        _txtDatabase = createTextField(138, 252, 160, 24);///*************

        add(_txtServerAddress);
        add(_txtPort);
        add(_txtUser);
        add(_txtPass);
        add(_txtChuoiKetNoi);///*************
        add(_txtDatabase);///*************

        _txtServerAddress.setText("127.0.0.1");
        _txtPort.setText("1234");
        _txtUser.setText("admin");
        _txtPass.setText("admin");
        _txtDatabase.setText("QuanLySinhVien");
    }

    private JSeparator createSeparator(int x, int y, int width, int height)
    {
        JSeparator sp = new JSeparator();
        sp.setPreferredSize(new Dimension(width, height));
        springLayout.putConstraint(SpringLayout.WEST, sp, x, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, sp, y, SpringLayout.NORTH, contentPane);
        return sp;
    }

    protected JButton createButton(String actionCommandButton, String altTextButton, int x, int y, int width, int height)
    {
        JButton btn = new JButton(altTextButton);
        btn.setPreferredSize(new Dimension(width, height));
        btn.setActionCommand(actionCommandButton);
        btn.addActionListener(this);
        springLayout.putConstraint(SpringLayout.WEST, btn, x, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, btn, y, SpringLayout.NORTH, contentPane);
        return btn;
    }

    protected JLabel createLabel(String altText, int x, int y)
    {
        JLabel label = new JLabel(altText);
        springLayout.putConstraint(SpringLayout.WEST, label, x, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, label, y, SpringLayout.NORTH, contentPane);
        return label;
    }

    private JTextField createTextField(int i, int i0, int width, int height)
    {
        JTextField txt = new JTextField();
        txt.setPreferredSize(new Dimension(width, height));
        springLayout.putConstraint(SpringLayout.WEST, txt, i, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, txt, i0, SpringLayout.NORTH, contentPane);
        return txt;
    }

    private JPasswordField createPassWordField(int i, int i0, int width, int height)
    {
        JPasswordField txt = new JPasswordField();
        txt.setPreferredSize(new Dimension(width, height));
        springLayout.putConstraint(SpringLayout.WEST, txt, i, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, txt, i0, SpringLayout.NORTH, contentPane);
        return txt;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        switch (actionCommand)
        {
            case "OK":
                if (checkNotNullOfTextFields())
                {
                    _frmParent.SetConnected(new MyConnectionString(_txtServerAddress.getText(), _txtPort.getText(), _txtDatabase.getText(), _txtUser.getText(), _txtPass.getText()));
                    this.dispose();
                }
                break;
            case "Cancel":
                this.dispose();
                break;
        }
    }

    private int getWidth(String _connecttoserverStr)
    {
        JLabel label = new JLabel(_connecttoserverStr);
        return label.getPreferredSize().width;
    }

    private boolean checkNotNullOfTextFields()
    {
        if (_txtServerAddress.getText().length() <= 0)
        {
            JOptionPane.showMessageDialog(this, "Vui lòng nhập Server Address!");
            return false;
        }
        if (_txtPort.getText().length() <= 0)
        {
            JOptionPane.showMessageDialog(this, "Vui lòng nhập Port!");
            return false;
        }
        if (_txtUser.getText().length() <= 0)
        {
            JOptionPane.showMessageDialog(this, "Vui lòng nhập UserName!");
            return false;
        }
        if (_txtPass.getText().length() <= 0)
        {
            JOptionPane.showMessageDialog(this, "Vui lòng nhập PassWord!");
            return false;
        }
        return true;
    }
}
